import React, {
    createContext,
    useContext,
    useState,
} from "react";
import {
    getEventList,
    getStatistics, getStatisticsEvents, getTimeSlot,
} from "../services/index";
import {ITimeSlot} from "../types/calculate";
import {IStatistics, IStatisticsMoods} from "../types/statistics";

interface StatisticsEventsContextData {
    eventObj: {},
    eventsName: '',
    statisticsEvents: IStatistics[];
    statisticsMoodsEvents: IStatisticsMoods[];
    setStatisticsEvents: ITimeSlot[];
    statisticsEventsData: [];
    loadStatisticsEvents: () => Promise<void>;
    loadEventList: () => Promise<void>;
    loadCalculateEventsChildren: (params: { endDate: string; type: string; events: any[]; startDate: string }) => Promise<void>;
    loadTimeSlotEvents: () => Promise<void>;
    setEventObjData: (parse: any) => {};
}

const StatisticsEventsContext = createContext({} as StatisticsEventsContextData);

interface CalculateProviderProps {
    children: React.ReactNode;
}

export const StatisticsEventsProvider: React.FC<CalculateProviderProps> = ({ children }) => {

    const [statisticsEvents, setStatisticsEvents] = useState([]);
    const [statisticsMoodsEvents, setStatisticsMoodsEvents] = useState([]);
    const [statisticsEventsData, setStatisticsEventsData] = useState([]);
    const [eventsName, setEventsName] = useState('');
    const [timeSlotEvents, setTimeSlotEvents] = useState([]);
    const [ eventObj, setEventObj ] = useState({});

    async function loadStatisticsEvents(params) {
        const res = await getStatistics(params);
        // setStatisticsEvents(res.data?.events||[]);
        // setStatisticsMoodsEvents(res.data?.moods||[]);
        // setEventsName(res.data?.eventName);
        return res;
    }

    async function loadTimeSlotEvents(params) {
        const res = await getTimeSlot(params);
        // setTimeSlotEvents(res.data?.dates||[]);
        return res;
    }

    async function loadStatisticsEventsData(params) {
        const res = await getStatisticsEvents(params);
        // setStatisticsEventsData(res.data?.events||[]);
        return res;
    }

    async function loadEventList() {
        const res = await getEventList();
        return res;
    }

    async function setEventObjData(obj) {
        setEventObj(obj);
    }

    return (
        <StatisticsEventsContext.Provider
            value={{
                eventObj,
                timeSlotEvents,
                eventsName,
                statisticsEvents,
                statisticsMoodsEvents,
                statisticsEventsData,
                loadStatisticsEvents,
                loadTimeSlotEvents,
                loadStatisticsEventsData,
                loadEventList,
                setEventObjData,
            }}
        >
            {children}
        </StatisticsEventsContext.Provider>
    );
};

export function useStatisticsEvents() {
    const context = useContext(StatisticsEventsContext);

    if (!context) throw new Error("useCalculate must be used inside a CalculateProvider");

    return context;
}
